﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace PBO.Game
{
    public class RndController
    {
        private Random rnd;

        public RndController(Random random)
        {
            this.rnd = random;
        }

        public RndController(int seed)
        {
            this.rnd = new Random(seed);
        }

        public double NextDouble()
        {
            return rnd.NextDouble();
        }

        public int Next(int n)
        {
            return rnd.Next(n);
        }

        public int Next(int min, int max)
        {
            return rnd.Next(min, max);
        }

        public T GetRandom<T>(IEnumerable<T> array, Random random)
        {
            int count = array.Count();
            return array.ElementAt(rnd.Next(count));
        }

        public int GetRandomInt(int min, int max)
        {
            return min == max ? min : rnd.Next(min, max + 1);
        }

        public bool RandomHappen(int percentage)
        {
            return rnd.Next(100) < percentage;
        }

        public bool OneNth(int n)
        {
            return rnd.Next(n) == 0;
        }
    }

    internal static class RandomHelper
    {
        public static bool OneNth(this Random random, int n)
        {
            return random.Next(n) == 0;
        }

        public static bool RandomHappen(this Random random, int percentage, int total = 100)
        {
            return random.Next(total) < percentage;
        }

        public static T GetRandom<T>(this Random random, IEnumerable<T> array)
        {
            int count = array.Count();
            return array.ElementAt(random.Next(count));
        }

        public static T GetRandom<T>(this Random random, T[] array)
        {
            if (array == null || array.Length == 0) return default(T);
            return array[random.Next(array.Length)];
        }

        public static T GetRandom<T>(this Random random, IList<T> list)
        {
            if (list == null || list.Count == 0) return default(T);
            return list[random.Next(list.Count)];
        }
    }
}
